import { getTodos } from './../services/todos.js';

export default {
    namespace: 'todos', //命名空间名字，必填
    state: { todos: [] }, //state就是用来放初始值的
    reducers: {
        //能改变界面的action应该放这里,这里按官方意思不应该做数据处理，只是用来return state 从而改变界面
        setList(state, { payload: list }) {
            //state是当前state,激变的结果是返回新的state，状态改变
            //console.log(state);
            let newState = JSON.parse(JSON.stringify(state));
            newState.todos = list;
            return newState;
        },
        delete(state, { payload: id }) {
            return state.filter(item => item.id !== id);
        },
    },
    effects: {
        *getList({ payload }, { call, put, select }) {
            const response = yield call(getTodos, payload);
            yield put({
                //触发reducers,put同时它也能触发同等级effects中其他方法。
                type: 'setList',
                payload: response.data.data,
            });
        },
    },
};
